// pages/search/search.js
const app = getApp()
var token = wx.getStorageSync('token');
Page({

  /**
   * 页面的初始数据
   */
  data: {
     caipin:[],
     isClassify:false,
     isPrice:false,
     classify:[],
     AccordingPrice:[],
     noClassify:[],
     searchClass:false,
     noprice:[],
     curr:0,
     curr2:0,
     name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    // 无搜索条件
    wx.request({
      url: app.globalData.directory + 'api/v2/getNoSearch', //仅为示例，并非真实的接口地址
      data: {
       
      },
      header: {
        'content-type': 'application/json', // 默认值
        'token': token
      },
      success: function (res) {
        // console.log(res.data.data.goods[0].sale_price, '价格！！！！！！')
        // console.log(Math.round(res.data.data.goods[0].sale_price), '价格！！！！！！')
        if (res.data.status == 'ok') {
          that.setData({
            caipin:res.data.data.goods,
            classify:res.data.data.category,
            AccordingPrice: res.data.data.searchPrice

          })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });
  
  },
 
  name:function(event){
    var name = event.detail.value;
    this.setData({
      name:name
    })
  },
  classify:function (e) {
     this.setData({
       isClassify:!this.data.isClassify,
       isPrice:false
     })
  },
  AccordingPrice:function (e){
    this.setData({
      isClassify:false,
      isPrice:!this.data.isPrice
    })
  },
  mvvmClass:function(e) {
    let index = e.currentTarget.dataset.index
    let classify = this.data.classify
    let name = classify[index].cates_name;
    var that=this;
    this.setData({
      isClassify: !this.data.isClassify,
      noClassify:name,
      curr:index
    })
    that.search();
  },
  mvvmPrice: function (e) {
    let index = e.currentTarget.dataset.index;
    let AccordingPrice = this.data.AccordingPrice;
    let price = AccordingPrice[index].show;
    var that=this;
    this.setData({
      isPrice: !this.data.isPrice,
      noprice: price,
      curr2: index
    })
    that.search();    
  },
  huigui:function (e) {
     this.setData({
       isClassify: false,
       isPrice: false,
      
     })
  },
  search: function () {
    var that = this;
    var price = that.data.AccordingPrice[that.data.curr2].price_section;
    var category = that.data.classify[that.data.curr].id;
    var name = that.data.name
    // 有搜索条件
    wx.request({
      url: app.globalData.directory + 'api/v2/getGoodsSearch',
      data: {
        price: price,
        category: category,
        name: name
      },
      method: "POST",
      header: {
        'content-type': 'application/json',
        'token': token
      },
      success: function (res) {
        if (res.data.status == 'ok') {
          if(res.data.data == ''){
            wx.showModal({
              title: "提示",
              content: "对不起,暂无此类商品.",
              showCancel: false,
              confirmText: "确定"
            })
          }else{
            that.setData({
              caipin: res.data.data,
            })
          }
         
          // that.setData({
          //   caipin: res.data.data,
          // })
        } else {
          wx.showToast({
            title: '请求失败,请稍候',
            icon: 'none',
            duration: 2000,
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  }

  
})